package editor;

import java.awt.BorderLayout;
import java.awt.Frame;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import javax.swing.JFrame;
import javax.swing.JSplitPane;
import javax.swing.UIManager;

import editor.manager.WorkSpaceManager;

/**
 * Main JWalkEditor Class
 *
 * @author Mark Dessain
 */
@SuppressWarnings("serial")
public class JWalkEditor extends JFrame implements WindowListener
{

	private Image jwalkIcon = Toolkit.getDefaultToolkit().getImage("graphics/walkerIcon.gif");
	
	/**
	 * @param args program arguments, none required
	 */
	public static void main(String[] args) 
	{
		// Sets the default look and feel for the OS
		try 
		{
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} 
		catch (Exception e) 
		{
		}
		
		// Display Editor
		Context.getJwalkEditor().display();
	}
	
	/**
	 * Empty
	 */
	public JWalkEditor(){}
	
	/**
	 * Sets-up the editor and sets the window to visible
	 */
	public void display()
	{
		// Sets up the main frame layout
		setup();

		this.setIconImage(jwalkIcon);
		
		// Sets frame state
		setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		addWindowListener(this);
		
		setSize(Context.getScreenDimensions().width, Context.getScreenDimensions().height);
		setExtendedState(Frame.MAXIMIZED_BOTH);
		setTitle("Syntax");	
		setVisible(true);
	}
	
	/**
	 * Adds a splitter to separate the sidebar from the workspace
	 */
	private void setup()
	{  
		// Sets main text-area
		JSplitPane mainSplit = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, Context.getSideBarManager(), Context.getWorkSpaceManager());

		// Add to frame
		add(mainSplit, BorderLayout.CENTER);   
		setJMenuBar(Context.getMenuManager());
	}

	/**
	 * Catches a close request and checks to see if all the files have been saved that need saving
	 * 
	 * (non-Javadoc)
	 * @see java.awt.event.WindowListener#windowClosing(java.awt.event.WindowEvent)
	 */
	@Override
	public void windowClosing(WindowEvent arg0) 
	{	
		if(WorkSpaceManager.textAreaManager.closeSave())
		{
			this.dispose();
		}
	}
	
	/* (non-Javadoc)
	 * @see java.awt.event.WindowListener#windowActivated(java.awt.event.WindowEvent)
	 */
	@Override
	public void windowActivated(WindowEvent arg0) {}
	
	/* (non-Javadoc)
	 * @see java.awt.event.WindowListener#windowClosed(java.awt.event.WindowEvent)
	 */
	@Override
	public void windowClosed(WindowEvent arg0) {}
	
	/* (non-Javadoc)
	 * @see java.awt.event.WindowListener#windowDeactivated(java.awt.event.WindowEvent)
	 */
	@Override
	public void windowDeactivated(WindowEvent arg0) {}

	/* (non-Javadoc)
	 * @see java.awt.event.WindowListener#windowDeiconified(java.awt.event.WindowEvent)
	 */
	@Override
	public void windowDeiconified(WindowEvent arg0) {}

	/* (non-Javadoc)
	 * @see java.awt.event.WindowListener#windowIconified(java.awt.event.WindowEvent)
	 */
	@Override
	public void windowIconified(WindowEvent arg0) {}

	/* (non-Javadoc)
	 * @see java.awt.event.WindowListener#windowOpened(java.awt.event.WindowEvent)
	 */
	@Override
	public void windowOpened(WindowEvent arg0) {}

}
